package com.example.alldemo.Filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class GlobleController{
    private static final Logger LOG = LoggerFactory.getLogger(GlobleController.class);

    @ExceptionHandler(Exception.class)
    public ModelAndView testException(Exception e, HttpServletRequest request,HttpServletResponse response) throws Exception{
        System.out.println("不好意思，请求出错了");
        e.printStackTrace();
        String localizedMessage = e.getLocalizedMessage();
        Map map = new HashMap();
        map.put("err",localizedMessage);
        //request.getRequestDispatcher("404.html").forward(request, response);
       //return  new ModelAndView("404",map);
        request.setAttribute("map",map);
        return  new ModelAndView("404");
    }
//
//    @ExceptionHandler(NullPointerException.class)
//    public ModelAndView npException(NullPointerException e){
//        System.out.println("空异常错误");
//        e.printStackTrace();
//        Map map = new HashMap();
//        map.put("err",e.getStackTrace());
//        return  new ModelAndView("404",map);
//    }


}
